const express = require('express')
const router = express.Router()
const StudentModel = require('../model/Student.Model')

router.get('/student', async (req, res) => {
  const arts = await StudentModel.findAllStudent()
  res.json(arts)
})

router.get('/student/:id', async (req, res) => {
  const id = req.params.id
  const art = await StudentModel.findStudentById(id)
  res.json(art)
})

router.post('/student', async (req, res) => {
  const obj = {
    sname: req.body.sname,
    snickname: req.body.snickname,
    sgender: req.body.sgender,
    sage: req.body.sage,
    sdept: req.body.sdept
  }
  const result = await StudentModel.addStudent(obj)
  res.json(result)
})

router.put('/student/:id', async (req, res) => {
  const obj = {
    sno: req.params.id,
    sname: req.body.sname,
    snickname: req.body.snickname,
    sgender: req.body.sgender,
    sage: req.body.sage,
    sdept: req.body.sdept
  }

  const result = await StudentModel.modifyStudent(obj)
  res.json(result)
})


router.delete('/student/:id', async (req, res) => {
  const id = req.params.id
  const result = await StudentModel.deleteStudentById(id)
  res.json(result)
})

module.exports = router
